package net.kazed.nextaction.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

public class TaskAttachment implements BaseColumns {
    public static final Uri CONTENT_URI = Uri.parse("content://" + GtdAndroid.PACKAGE + "/taskAttachment");
    public static final Uri URI_ATTACHMENTS_BY_TASK = Uri.parse("content://" + GtdAndroid.PACKAGE + "/taskAttachmentsByTask");
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.nextaction.taskAttachment";
    public static final String DEFAULT_SORT_ORDER = "type ASC";

    public static class Column {
        public static final String ID = _ID;
        public static final String TASK_ID = "taskId";
        public static final String TYPE = "type";
        public static final String URL = "url";
    }

    /**
     * Projection for all the columns of a task attachment.
     */
    public static final Projection FULL_PROJECTION = new Projection(new String[] { Column.ID, Column.TASK_ID, Column.TYPE,
                    Column.URL });

    private Integer id;
    private Integer taskId;
    private String type;
    private String url;

    /**
     * Constructor.
     * @param taskId Task ID.
     * @param type Attachment type.
     * @param url URL.
     */
    public TaskAttachment(Integer taskId, String type, String url) {
        super();
        this.taskId = taskId;
        this.type = type;
        this.url = url;
    }

    /**
     * Constructor.
     * @param cursor Database cursor.
     */
    public TaskAttachment(Cursor cursor) {
        Projection projection = TaskAttachment.FULL_PROJECTION;
        this.id = projection.getInteger(cursor, Column.ID);
        this.taskId = projection.getInteger(cursor, Column.TASK_ID);
        this.type = projection.getString(cursor, Column.TYPE, "");
        this.url = projection.getString(cursor, Column.URL, "");
    }

    /**
     * @return ID.
     */
    public Integer getId() {
        return id;
    }

    /**
     * @return Task ID.
     */
    public Integer getTaskId() {
        return taskId;
    }

    /**
     * @param taskId Task ID.
     */
    public void setTaskId(Integer taskId) {
        this.taskId = taskId;
    }

    /**
     * @return Attachment type.
     */
    public String getType() {
        return type;
    }

    /**
     * @param type Attachment type.
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return URL.
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url URL.
     */
    public void setUrl(String url) {
        this.url = url;
    }
    
    /**
     * Copy attributes to values.
     * @param values Values of model attributes.
     */
    public void toValues(ContentValues values) {
        values.put(Column.TASK_ID, taskId);
        values.put(Column.TYPE, type);
        values.put(Column.URL, url);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TaskAttachment other = (TaskAttachment) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }


}
